#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@author:atuo.cn
'''

from functools import partial

class DialogHelper:
            
    def _dispatchevents(self, handler, event):
        try:
            doevent=getattr(self, handler)
            assert callable(doevent)
            doevent(event)
        except AttributeError:
            pass
        
    def _dispatchcmd(self, cmd):
        try:
            cmd=getattr(self, cmd)
            assert callable(cmd)
            cmd()
        except AttributeError:
            pass    
    
    def _eventhandler(self,handler):
        return partial(self._dispatchevents, handler)

    def _cmdhandler(self,cmd):
        return partial(self._dispatchcmd, cmd)
        
    @staticmethod
    def centerWindow(window, show=True):
        window.withdraw()
        # Update "requested size" from geometry manager
        window.update_idletasks()
        
        s_width = window.winfo_screenwidth()
        s_height = window.winfo_screenheight()
        #use winfo_reqwidth/reqheight instead of winfo_width/height
        w_width = window.winfo_reqwidth()
        w_height = window.winfo_reqheight()
      
        x = (s_width - w_width) / 2
        y = (s_height - w_height) / 2
      
        window.geometry("+%d+%d"%(x,y))
        show and window.deiconify()
